package finman.database;
import java.util.*;

class Bank {
	private String name;
	private Set<Schet> scheta = new HashSet<Schet>();
	
	Bank(String name) {
		this.name = name;		
	}
	
	/**
	 * @return String that represents a number of new account.
	 */
	public String openAccount(Valuta valuta, Double initialBalance) {		
		Schet newSchet = new Schet(getFreeAccountNumber(), valuta, initialBalance);
		scheta.add(newSchet);
		return newSchet.getNumber();
	}
	
	private String getFreeAccountNumber() {		
		if(scheta.size() < 1) {
			return Schet.getNextAccountNumber(null);
		}		
		String freeAccountNumber = null;		
		for(Schet schet: scheta) {
			if(freeAccountNumber == null) {
				freeAccountNumber = schet.getNumber();
			} else {
				if(schet.compareNumberTo(freeAccountNumber) > 0) {
					freeAccountNumber = schet.getNumber();
				}
			}			
		}
		return Schet.getNextAccountNumber(freeAccountNumber);
	}
}